package com.hong.utils.condition;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/7/23 17:31
 * @Description:
 */
public class ComBetweenThreads {

    private static int data = 0;

    private static boolean noUse = true;

    private final static Object MONITOR  = new Object();

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                for (;;){
                 buildData();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                for (;;){
                    useData();
                }
            }
        }.start();
    }

    private static void sleep(long seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void buildData() {
        synchronized (MONITOR){
            while (noUse){
                try {
                    MONITOR.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            data++;
            sleep(1);
            System.out.println("P==>"+data);
            noUse= true;
            MONITOR.notifyAll();
        }
    }

    private static void useData(){
        synchronized (MONITOR){
            while (!noUse){
                try {
                    MONITOR.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("C=>"+data);
            noUse=false;
            MONITOR.notifyAll();
        }
    }
}
